/*
 * Copyright (C) 2010-2012 Susanta Tewari
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command;

import cef.command.Command;
import cef.command.CommandException;
import coalescent.cef.event.MLE_KC64ThetaEvent;
import coalescent.model.KC64;
import coalescent.recursion.ProbComputer_Exact_Multi;
import coalescent.recursion.Recursion;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @since 1.4.0
 * @version version
 * @author Susanta Tewari
 * @history Created on Dec 18, 2012.
 */
public class MLE_KC64ThetaCommand extends Command<MLE_KC64ThetaEvent> {

    /** theta-probability */
    protected Map<Double, BigDecimal> thetaProbabilities;

    public MLE_KC64ThetaCommand(final MLE_KC64ThetaEvent event) {
        super(event);
    }

    @Override
    public void executeImpl() throws CommandException, InterruptedException {


        // models
        final List<Double> thetas = thetaValues(getEvent().getThetaMin(), getEvent().getThetaMax(),
                                        getEvent().getThetaIncrement());
        final List<KC64> models = new ArrayList<>(thetas.size());

        for (final double theta : thetas) {
            models.add(new KC64(theta));
        }


        // recursion
        final Recursion recursion                         =
            new Recursion(getEvent().getStatistic());
        final ProbComputer_Exact_Multi<KC64> probComputer = new ProbComputer_Exact_Multi<>(models);

        recursion.addExactRecursionEventListener(probComputer);
        recursion.runRecursion();


        // collect theta-probs
        thetaProbabilities = new HashMap<>(thetas.size());

        final Map<KC64, BigDecimal> prob_map = probComputer.getResult();

        for (final KC64 model : prob_map.keySet()) {
            thetaProbabilities.put(model.getMutationRate(), prob_map.get(model));
        }
    }

    /**
     * Method description
     *
     * @return description
     */
    public Map<Double, BigDecimal> getThetaProbabilities() {

        checkCmdFinished();

        return thetaProbabilities;
    }

    /**
     * Method description
     *
     * @param low description
     * @param high description
     * @param increment description
     *
     * @return description
     */
    protected List<Double> thetaValues(final double low, final double high,
                                       final double increment) {

        final List<Double> result = new ArrayList<>();
        double val                = low;

        while (val <= high) {

            result.add(val);

            val += increment;
        }

        return result;
    }
}
